package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Product;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.query.EmpQuery;
import com.by.common.query.ProductQuery;
import com.by.service.ProductService;
import io.swagger.annotations.*;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Ye QS
 * @version 0.1
 * @className ProductController
 * @description
 * @date 2024/9/14 19:27
 * @since jdk11
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "产品管理")
public class ProductController extends BaseController {
    @Autowired
    ProductService productService;

    @ApiOperation("查询所有")
    @GetMapping("/product/all")
    public ResultBean queryAll() {
        List<Product> products = productService.queryAll();
        return success("产品列表", products);
    }

    @ApiOperation("新增产品")
    @PostMapping("/product")
    public ResultBean save(@RequestBody Product product) {
        int res = productService.save(product);
        return saveResult(res);
    }

    @ApiImplicitParam(name = "id", value = "产品id", required = true, defaultValue = "0", dataTypeClass = Integer.class)
    @ApiOperation("删除产品")
    @DeleteMapping("/product")
    public ResultBean delete(@ApiParam(name = "id", required = true, value = "要删除的id") @RequestParam(name = "id", required = true, defaultValue = "0") int id) {
        int delete = productService.delete(id);
        return deleteResult(delete);
    }

    @ApiOperation("模糊搜索")
    @PostMapping("/product/query")
    public ResultBean query(@RequestBody ProductQuery query) {
        // 将传入的 EmpQuery 对象转换成 Map 结构，便于后续处理使用。
        Map<String, Object> map = BeanUtil.beanToMap(query);

        // 调用 employeeService 中定义的方法 query 进行员工信息查询，并接收返回的分页数据。
        PageBean pageBean = productService.query(map);

        // 构造并返回一个包含自定义消息和实际查询结果的成功响应对象。
        return success("产品列表", pageBean);
    }
    @ApiOperation("产品详情")
    @GetMapping("/product")
    public ResultBean getById(@RequestParam(value = "id", required = true) int id) {
        Product product = productService.get(id);
        return success("产品详情", product);
    }
}
